package org.csu.mypetstorecmsapi.controller;

import org.csu.mypetstorecmsapi.common.BaseResponse;
import org.csu.mypetstorecmsapi.domain.Category;
import org.csu.mypetstorecmsapi.domain.Item;
import org.csu.mypetstorecmsapi.domain.Product;
import org.csu.mypetstorecmsapi.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    //获取信息列表
    @GetMapping("/categories")
    public BaseResponse<Object> getCategories() {
        return BaseResponse.createForSuccess(catalogService.getCategories());
    }

    @GetMapping("/categories/{id}/products")
    public BaseResponse<Object> getProducts(@PathVariable int id) {
        String categoryId = catalogService.getCategoryById(id).getCategoryId();
        return BaseResponse.createForSuccess(catalogService.getProducts(categoryId));
    }

    @GetMapping("/products/{id}/items")
    public BaseResponse<Object> getItems(@PathVariable int id) {
        String productId = catalogService.getProductById(id).getProductId();
        return BaseResponse.createForSuccess(catalogService.getItems(productId));
    }

    //新增
    @PostMapping("/categories")
    public BaseResponse<Object> addCategory(@RequestBody Category category) {
        catalogService.addCategory(category);
        return BaseResponse.createForSuccess();
    }

    @PostMapping("/categories/{id}/products")
    public BaseResponse<Object> addProduct(@PathVariable int id, @RequestBody Product product) {
        product.setCategoryId(catalogService.getCategoryById(id).getCategoryId());
        catalogService.addProduct(product);
        return BaseResponse.createForSuccess();
    }

    @PostMapping("/products/{id}/items")
    public BaseResponse<Object> addItem(@PathVariable int id, @RequestBody Item item) {
        item.setProductId(catalogService.getProductById(id).getProductId());
        catalogService.addItem(item);
        return BaseResponse.createForSuccess();
    }

    //删除
    @DeleteMapping("/categories/{id}")
    public BaseResponse<Object> deleteCategory(@PathVariable int id) {
        String categoryId = catalogService.getCategoryById(id).getCategoryId();
        catalogService.deleteProductByCategoryId(categoryId);
        catalogService.deleteItemByCategoryId(categoryId);
        catalogService.deleteCategory(id);
        return BaseResponse.createForSuccess();
    }

    @DeleteMapping("/categories/{id1}/products/{id2}")
    public BaseResponse<Object> deleteProduct(@PathVariable int id1, @PathVariable int id2) {
        String productId = catalogService.getProductById(id2).getProductId();
        catalogService.deleteItemByProductId(productId);
        catalogService.deleteProduct(id2);
        return BaseResponse.createForSuccess();
    }

    @DeleteMapping("/products/{id1}/items/{id2}")
    public BaseResponse<Object> deleteItem(@PathVariable int id1, @PathVariable int id2) {
        catalogService.deleteItem(id2);
        return BaseResponse.createForSuccess();
    }

    //修改item信息
    @PutMapping("/products/{id1}/items/{id2}")
    public BaseResponse<Object> editItem(@PathVariable int id1, @PathVariable int id2, @RequestBody Item item) {
        catalogService.editItem(id2, item);
        return BaseResponse.createForSuccess();
    }
}
